package com.chixing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.commons.Result;
import com.chixing.commons.ServerResponse;
import com.chixing.commons.TokenUtil;
import com.chixing.mapper.EducationalExperienceMapper;
import com.chixing.mapper.ExperienceProjectMapper;
import com.chixing.mapper.InternshipExperienceMapper;
import com.chixing.mapper.MyresumeMapper;
import com.chixing.pojo.*;
import com.chixing.service.ICompanyDetailService;
import com.chixing.service.IJobDetailService;
import com.chixing.service.IMyresumeService;
import com.chixing.service.IPersonCenterService;
import com.chixing.service.impl.MyresumeServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 简历表 前端控制器
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@RestController
public class MyresumeController {
    @Autowired
    IMyresumeService myresumeService;
    @Autowired
    IPersonCenterService personCenterService;

    @Autowired
    private InternshipExperienceMapper internshipExperienceMapper;

    @Autowired
    private MyresumeMapper myresumeMapper;

    @Autowired
    private EducationalExperienceMapper experienceMapper;

    @Autowired
    private ExperienceProjectMapper experienceProjectMapper;

    //    用户登录加载myresume表数据
    @GetMapping ("cvLoadData")
    public ServerResponse getById(HttpServletRequest servletRequest){
        String token = servletRequest.getHeader("token");
        LoginPerson tokens=TokenUtil.parseToken(token); //将存储的token数据转换放入loginPerson
        System.out.println("tokens："+tokens);
        if (tokens !=null){
            System.out.println(tokens.getPersonId()+"AAAAAAAAAAAAAAAAAAAAAAAAA");
            Myresume result = myresumeService.getAllById(tokens.getPersonId());//将token数据全部查出来
            System.out.println(result+"AAAAAAAAAAAAAAAAAAAAAAAAA");
            return ServerResponse.success("查询myresume表成功",result);
        }
        return ServerResponse.success("查询myresume表失败",null);
    }

    //    根据myresumeId加载个人中心表数据
    @RequestMapping("/getPerNameByPerId")
    public ServerResponse getPerNameById(@RequestParam("personId") Integer personId){
        PersonCenter personCenter = personCenterService.getPerName(personId);
        return ServerResponse.success("查询用户表成功",personCenter);
    }

    //更新myresume表
    @RequestMapping("updateMyresume/{myResumeId}")
    public ServerResponse updateMyresume(Myresume myresume,@PathVariable("myResumeId") Integer myresumeId){
        ServerResponse serverResponse = myresumeService.updateMyresumer(myresume,myresumeId);

        return serverResponse;
    }

    //更新PersonCenter表
    @RequestMapping("updatePersonCenter")
    public ServerResponse updatePersonCenter(PersonCenter personCenter, HttpServletRequest request){
        System.out.println(personCenter);
        String token = request.getHeader("token");
        LoginPerson loginPerson = TokenUtil.parseToken(token);
        ServerResponse serverResponse = personCenterService.updatePersonCenter(personCenter, loginPerson.getPersonId());
        return serverResponse;
    }

    //根据用户id创建Myresume表，再根据myresumeId创建实习经历表和项目经历表
    @RequestMapping("insertMyResume")
    public ServerResponse insertMyResume(HttpServletRequest request){
        String token = request.getHeader("token");
        //先创建实习经历表，根据实习表id与个人id创建简历表
        Integer internshipId = null;//实习表id
        Integer personCenterId = null;//个人id
        //创建实习经历表并拿到该条记录的id（实习经历表id）
        String uuid = UUID.randomUUID().toString().replace("-","");
        InternshipExperience internshipExperience = new InternshipExperience();
        internshipExperience.setCompanyId(2101);//新人注册，统一在百度公司
        internshipExperience.setDepartmentName("研发部");
        internshipExperience.setJobName("需求文档编写");
        internshipExperience.setText2(uuid);
        Integer insertInternship = internshipExperienceMapper.insert(internshipExperience);
        //拿到该条记录的id
        QueryWrapper<InternshipExperience> internshipExperienceQueryWrapper = new QueryWrapper<>();
        internshipExperienceQueryWrapper.eq("text2",uuid);
        InternshipExperience internshipExperience1 = internshipExperienceMapper.selectOne(internshipExperienceQueryWrapper);
        internshipId = internshipExperience1.getInternshipId();

        //拿到个人id
        LoginPerson loginPerson = TokenUtil.parseToken(token);
        personCenterId = loginPerson.getPersonId();

        //创建Myresume表
        Myresume myresume = new Myresume();
        myresume.setInternshipId(internshipId);
        myresume.setPersonId(personCenterId);
        myresume.setExpectationSalaryMax(8000f);
        myresume.setExpectationSalaryMin(6000f);
        myresume.setTest1("英语四级");
        Integer insertMyResume = myresumeMapper.insert(myresume);
        QueryWrapper<Myresume> myresumeQueryWrapper = new QueryWrapper<>();
        myresumeQueryWrapper.eq("person_id",personCenterId).eq("internship_id",internshipId);
        Myresume myresumeSelect = myresumeMapper.selectOne(myresumeQueryWrapper);
        // 拿muresumeId创建教育经历表
        Integer myresumeId = myresumeSelect.getMyresumeId();
        EducationalExperience experience  = new EducationalExperience();
        experience.setMyresumeId(myresumeId);
        Integer insertEducationalExperience = experienceMapper.insert(experience);
        // 拿muresumeId创建项目经历表
//        Integer myresumeId = myresumeSelect.getMyresumeId();//拿到新创建的myresumeId
        ExperienceProject experienceProject = new ExperienceProject();
        experienceProject.setMyresumeId(myresumeId);
        Integer insertExperienceProject = experienceProjectMapper.insert(experienceProject);


        if(myresumeSelect!=null) {
            return ServerResponse.success("插入简历表成功", myresumeSelect);
        }
        return ServerResponse.fail("插入简历表失败",null);
    }



}
